package com.atguigu.gulimall.order.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

/**
 * MQ配置
 * 容器中的Binding Queue Exchange都会自动创建
 *
 * @author LaiSanShan
 * @date 2021-09-07
 */
@Configuration
public class MyMQConfig {

    /**
     * 30min后消息会发到order-event-exchange路由，带键order.release.order
     *
     * @return 延迟队列
     */
    @Bean
    public Queue orderDelayQueue() {
        HashMap<String, Object> arguments = new HashMap<>();
        arguments.put("x-dead-letter-exchange", "order-event-exchange");
        arguments.put("x-dead-letter-routing-key", "order.release.order");
        arguments.put("x-message-ttl", 60000);
        return new Queue("order.delay.queue",
                true,
                false,
                false,
                arguments);
    }

    /**
     * 接收30min后的订单消息
     *
     * @return 队列
     */
    @Bean
    public Queue orderReleaseOrderQueue() {
        return new Queue("order.release.order.queue",
                true,
                false,
                false);
    }

    /**
     * order服务事件的交换机
     *
     * @return 交换机
     */
    @Bean
    public Exchange orderEventExchange() {
        return new TopicExchange("order-event-exchange",
                true,
                false);
    }

    /**
     * order-event-exchange交换机到order.delay.queue，根据键order.create.order
     *
     * @return 交换机->队列
     */
    @Bean
    public Binding orderCreateOrderBinding() {
        return new Binding("order.delay.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.create.order",
                null);
    }

    /**
     * order-event-exchange交换机到order.release.order.queue，根据键order.release.order
     *
     * @return 交换机->队列
     */
    @Bean
    public Binding orderReleaseOrderBinding() {
        return new Binding("order.release.order.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.release.order",
                null);
    }

    /**
     * 订单释放直接和库存释放进行绑定，订单释放后发送消息给库存释放队列进行解锁库存
     *
     * @return 队列->队列
     */
    @Bean
    public Binding orderReleaseOtherBinding() {
        return new Binding("stock.release.stock.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.release.other.#",
                null);
    }

//    /**
//     * 监听 接收30min后的订单消息 的队列
//     *
//     * @param orderEntity 消息格式是订单实体
//     */
//    @RabbitListener(queues = "order.release.order.queue")
//    public void listener(OrderEntity orderEntity, Channel channel, Message message) throws IOException {
//        System.out.println("收到过期的订单信息，准备关闭订单：" + orderEntity.getOrderSn());
//        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
//    }

}
